//
//  TL_FeedbackPhotoManager.m
//  WXReader
//
//  Created by Andrew on 2019/12/28.
//  Copyright © 2019 Andrew. All rights reserved.
//

#import "TL_FeedbackPhotoManager.h"

@interface TL_FeedbackPhotoManager ()

@property (nonatomic, strong) NSMutableSet *uploadSet;

@end

@implementation TL_FeedbackPhotoManager

SINGLETON_IMPLEMENTATION(TL_FeedbackPhotoManager)

- (instancetype)init
{
    if (self = [super init]) {
        self.uploadSet = [NSMutableSet set];
    }
    return self;
}

- (void)addPhotoImage:(UIImage *)image complete:(void(^)(TL_FeedbackPhotoModel *photoModel))completeBlock
{
    
    MJWeakSelf
    [[MKAliyunOSSUtils sharedInstance] uploadImage:image type:CGUploadImageType_feedback block:^(NSString * _Nonnull url) {
        
        NSLog(@"上传地址：%@",url);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (url) {
                
                TL_FeedbackPhotoModel *pModel = [TL_FeedbackPhotoModel new];
                pModel.img = url;
                pModel.show_img = url;
                !completeBlock ?: completeBlock(pModel);
                
            }else{
                
                [TL_TopAlertManager showAlertWithType:TL_TopAlertTypeError alertTitle:WXLocalizedString(@"图片上传失败")];
                !completeBlock ?: completeBlock(nil);
                
            }
            
            if (weakSelf.uploadSet.count > 0) {
                [weakSelf.uploadSet removeObject:[weakSelf.uploadSet anyObject]];
            }
            
        });
        
    }];
    
}

- (BOOL)isUploading
{
    return self.uploadSet.count > 0;
}

- (void)deletePhotoWithPhotoModel:(TL_FeedbackPhotoModel *)photoModel
{
    if (photoModel.show_img.length <= 0) {
        [TL_TopAlertManager showAlertWithType:TL_TopAlertTypeError alertTitle:WXLocalizedString(@"图片删除失败")];
        return;
    }
    
    if (photoModel) {
        if (self.deletePhotoBlock) {
            self.deletePhotoBlock(photoModel);
        }
    }
}

@end

